﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Entities;
using NHibernate;
using Totoro.ORMapping;

namespace Bussiness.ModelQuery
{
    public class OrderDetailManager
    {
        public OrderDetailManager()
        {
 
        }
        private static OrderDetailManager instace;

        public static OrderDetailManager Instace
        {
            get 
            {
                if (instace == null)
                {
                    instace = new OrderDetailManager();
                    instace.LoadData();
                }
                return instace; 
            }
        }
        Dictionary<string, OrderDetail> orderDetailCaches = new Dictionary<string, OrderDetail>();
        /// <summary>
        /// 加载所有详细订单信息
        /// </summary>
        public void LoadData()
        {
            orderDetailCaches.Clear();
            ISession session = NHHelper.GetCurrentSession();
            ICriteria crt = session.CreateCriteria<Products>();
            crt.AddOrder(new NHibernate.Criterion.Order("CreatTime", true));//按创建时间的升序排列
            IList<OrderDetail> orderDetailList = null;
            orderDetailList = crt.List<OrderDetail>();
            if (orderDetailList != null)
            {
                foreach (OrderDetail orderDetail in orderDetailList)
                {
                    orderDetailCaches.Add(orderDetail.ID, orderDetail);
                }
            }
        }
        /// <summary>
        /// 根据订单明细Id获取订单明细
        /// </summary>
        /// <param name="sId">订单明细Id</param>
        /// <returns></returns>
        public OrderDetail GetOrderDetailById(string sId)
        {
            if (orderDetailCaches.ContainsKey(sId))
            {
                return orderDetailCaches[sId];
            }
            return null;
        }
        /// <summary>
        /// 获取所有的明细信息
        /// </summary>
        /// <returns></returns>
        public OrderDetail[] GetOrderDetails()
        {
            return orderDetailCaches.Values.ToArray();
        }
        /// <summary>
        /// 根据主订单获取订单信息
        /// </summary>
        /// <param name="sOrderId">主订单信息</param>
        /// <returns></returns>
        public OrderDetail[] GetOrderDetailsByOrderId(string sOrderId)
        {
            List<OrderDetail> detailList = new List<OrderDetail>();
            foreach (OrderDetail orderDetail in orderDetailCaches.Values)
            {
                if (orderDetail.OrderID == sOrderId)
                {
                    detailList.Add(orderDetail);
                }
            }
            return detailList.ToArray();
        }
        /// <summary>
        /// 根据产品Id获取产品订单信息
        /// </summary>
        /// <param name="sProductId"></param>
        /// <returns></returns>
        public OrderDetail[] GetOrderDetailsByProductId(string sProductId)
        {
            List<OrderDetail> detailList = new List<OrderDetail>();
            foreach (OrderDetail orderDetail in orderDetailCaches.Values)
            {
                if (orderDetail.ProductID == sProductId)
                {
                    detailList.Add(orderDetail);
                }
            }
            return detailList.ToArray();
        }
        /// <summary>
        /// 获取某个产品的总订单数
        /// </summary>
        /// <param name="sProductId">产品Id</param>
        /// <returns>总订单数</returns>
        public int GetOrderCountByProductId(string sProductId)
        {
            int count = 0;
            foreach (OrderDetail orderDetail in orderDetailCaches.Values)
            {
                if (orderDetail.ProductID == sProductId)
                {
                    count += orderDetail.Quantity;
                }
            }
            return count;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sId"></param>
        public void DeleteOrderDetailByID(string sId)
        {
            ISession session = NHHelper.GetCurrentSession();
            OrderDetail order = GetOrderDetailById(sId);
            if (order != null)
            {
                session.Delete(order);
                orderDetailCaches.Remove(sId);
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="order"></param>
        public void InsertOrder(OrderDetail orderDetail)
        {
            NHHelper.SaveOjb(orderDetail);
            orderDetailCaches.Add(orderDetail.ID, orderDetail);
        }
    
    }
}
